/****************************************************************
 *
 *  Project:
 *  Description: 
 *  Id:
 *  Date: 04/May/2010
 *  Copyright (c) Wellcome Trust  Sanger Institute 2010
 *
 *************************************************************** */
package uk.ac.sanger.solr;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.mydas.examples.solr.Int;
import uk.ac.ebi.mydas.examples.solr.Response;
import uk.ac.ebi.mydas.examples.solr.Result;
import uk.ac.ebi.mydas.examples.solr.Result.Doc;
import uk.ac.ebi.mydas.examples.solr.Str;
import uk.ac.sanger.das.dasregistry.springrest.sources.SourcesController;
import uk.ac.sanger.www.WTSISearchService.WTSISearchService_PortType;
import uk.ac.sanger.www.WTSISearchService.WTSISearchService_ServiceLocator;

/**
 * 
 * @author mw8
 */
public class GetResultsFromSanger {
	private static final String solrBaseUrl = "http://web-solr-dev.internal.sanger.ac.uk:8000/solr-sanger/dasregistry/";
	private enum QueryType {dasWS,dastypes};
	static Logger logger = Logger.getLogger(GetResultsFromSanger.class);

	public static void main(String args[]) {
		GetResultsFromSanger sanger = new GetResultsFromSanger();
		// sanger.getSourcesWithTypesLikeKeywordsSearch("exon" );
		sanger.getSourcesForKeywordsSearch("exon");
	}

	public List<String> getSourcesForKeywordsSearch(String query) {

		List<String> ids = this.getIdsForDomain(query, "DS_", QueryType.dasWS);
		return ids;
	}

	public List<String> getCoordinatesForKeyWords(String query) {

		List<String> ids = this.getIdsForDomain(query, "CS_DS", QueryType.dasWS);
		return ids;
	}

	public List<String> getSourcesWithTypesLikeKeywordsSearch(String query) {
		//typeId field searched only although we need to search the cvId field as well.
		List<String> ids = this.getIdsForDomain(query, "DS_", QueryType.dastypes);
		return ids;
		// now I want to generate a list of sources for these ids, probably want
		// a public requet to be able to do this

		// pass this list of sources back to the calling servlet which can then
		// write them out

	}

	private List<String> getIdsForDomain(String keywords, String domain, QueryType queryType) {
		
		RestTemplate template = new RestTemplate();
		// logger.debug("url to solr=" + url);
		// try {
		// url=UriUtils.encodeUri(url, "UTF-8");
		// } catch (UnsupportedEncodingException e2) {
		// // TODO Auto-generated catch block
		// e2.printStackTrace();
		// }
		// solr query for all sources
		// "http://web-solr-dev.internal.sanger.ac.uk:8000/solr-sanger/dasregistry/select?q=*:*&facet=true&facet.field=domain&facet.limit=20&facet.mincount=1&json.nl=map&json.wrf=jQuery16409288456714712083_1332344609207&_=1332344609327";
		String url = solrBaseUrl +queryType+"?q="+ keywords;
		logger.debug("url to solr after encoding=" + url);
		Response response = null;
		try {
			response = template.getForObject(url, Response.class);
		} catch (RestClientException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		// parse the solr xml so we can generate DAS features
		Result result = response.getResult();

		int number = result.getNumFound();
		logger.debug("number of results found in solr " + number);
		List<Doc> docs = result.getDoc();
		List<String> ids = new ArrayList<String>();
		for (Doc doc : docs) {

			String id = "";

			List<Object> fields = doc.getStrOrIntOrFloat();
			for (Object field : fields) {
				if (field instanceof Str) {
					Str str = (Str) field;
					String name = str.getName();
					
					if (name.equals("id")) {
						id = str.getValue();
						if (id.contains(domain)) {
							ids.add(id);
						}

					}
				}
				
			}

		}
		logger.debug("ids size new solr method=" + ids.size());
		return ids;
	}

}
